/*
 * Copyright (c) 2009 Frederic Daoud
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package org.stripesbook.rayures.view.jsp;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.el.ExpressionEvaluator;
import javax.servlet.jsp.el.VariableResolver;

/**
 * A minimal implementation of {@link PageContext}. This class holds instances of the request,
 * response, session, servlet context, and servlet config, and uses a {@link ViewJspWriter} as an
 * implementation of {@link JspWriter}. Methods that deal with these objects are implemented, but
 * just about all of the other methods are not used and therefore are empty/return null.
 *
 * @author Frederic Daoud
 */
public class JspPageContext extends PageContext {
  private JspWriter writer = new ViewJspWriter();
  private ServletRequest request;
  private ServletResponse response;
  private HttpSession session;
  private ServletContext servletContext;
  private ServletConfig servletConfig;
  private Map<String, Object> attributes = new HashMap<String, Object>();

  public JspPageContext(HttpServletRequest req, HttpServletResponse resp) {
    request = req;
    response = resp;
    session = req.getSession();
    servletContext = session.getServletContext();
  }

  @Override
  public JspWriter getOut() {
    return writer;
  }

  @Override
  public void initialize(Servlet servlet, ServletRequest req, ServletResponse resp,
    String errorPageUrl, boolean needsSession, int bufferSize, boolean autoFlush)
    throws IOException, IllegalStateException, IllegalArgumentException
  {
  }

  @Override
  public HttpSession getSession() {
    return session;
  }

  @Override
  public ServletRequest getRequest() {
    return request;
  }

  @Override
  public ServletResponse getResponse() {
    return response;
  }

  @Override
  public ServletConfig getServletConfig() {
    return servletConfig;
  }

  @Override
  public ServletContext getServletContext() {
    return servletContext;
  }

  @Override
  public void setAttribute(String name, Object value) {
    attributes.put(name, value);
  }

  @Override
  public void setAttribute(String name, Object value, int scope) {
    attributes.put(name, value);
  }

  @Override
  public Object getAttribute(String name) {
    return attributes.get(name);
  }

  @Override
  public Object getAttribute(String name, int scope) {
    return attributes.get(name);
  }

  @Override
  public Object findAttribute(String name) {
    return attributes.get(name);
  }

  @Override
  public void removeAttribute(String name) {
    attributes.remove(name);
  }

  @Override
  public void removeAttribute(String name, int scope) {
    attributes.remove(name);
  }

  /* Methods below this point are empty/return null. */

  @Override
  public void release() {
  }

  @Override
  public void forward(String path) throws ServletException, IOException {
  }

  @Override
  public void include(String path) throws ServletException, IOException {
  }

  @Override
  public void include(String path, boolean flush) throws ServletException, IOException {
  }

  @Override
  public void handlePageException(Exception exc) throws ServletException, IOException {
  }

  @Override
  public void handlePageException(Throwable t) throws ServletException, IOException {
  }

  @Override
  public int getAttributesScope(String name) {
    return 0;
  }

  @Override
  public Enumeration<?> getAttributeNamesInScope(int scope) {
    return null;
  }

  @Override
  public ExpressionEvaluator getExpressionEvaluator() {
    return null;
  }

  @Override
  public VariableResolver getVariableResolver() {
    return null;
  }

  @Override
  public Object getPage() {
    return null;
  }

  @Override
  public Exception getException() {
    return null;
  }
}
